package com.example.modules.user;

import com.example.generator.model.User;
import com.example.modules.role.RoleVO;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Setter
public class UserDetailsVO extends User implements UserDetails {
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private List<RoleVO> roleVOList;

    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @Override
    public String getPassword() {
        return super.getPassword();
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return roleVOList.stream()
                .filter(s -> !ObjectUtils.isEmpty(s.getResourceList()))
                .flatMap(s -> s.getResourceList().stream())
                .map(s -> (GrantedAuthority) s::getName)
                .collect(Collectors.toList());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.getEnable();
    }
}
